package com.cater.sys.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.project.entity.DemandCommunicate;
import com.cater.project.entity.DemandInterview;
import com.cater.project.entity.DemandOfferRecord;
import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.project.service.DemandInterviewServiceImpl;
import com.cater.project.service.DemandOfferRecordServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.report.util.RemindDateUtils;
import com.cater.sys.entity.CompanyBook;
import com.cater.sys.entity.Role;
import com.cater.sys.entity.User;
import com.cater.sys.service.CompanyBookServiceImpl;
import com.cater.sys.service.RoleService;
import com.cater.sys.service.UserService;
import com.cater.talents.entity.TalentsRecommend;
import com.cater.talents.entity.WorkSearch;
import com.cater.talents.service.ResumeServiceImpl;
import com.cater.talents.service.TalentsRecommendServiceImpl;
import com.cater.talents.service.WorkSearchServiceImpl;
import com.cater.task.entity.TaskManager;
import com.cater.task.service.TaskManagerServiceImpl;

/**
 * 
 * @描述:首页
 * @作者 :ShenDong
 * @创建日期 :2016年12月7日8:41
 *
 */
@Controller
@RequestMapping("/index")
public class IndexController {

	@Autowired
	UserService userService;

	@Autowired
	RoleService roleService;

	@Autowired
	private ResumeServiceImpl resumeService;// 简历
	@Autowired
	private DemandCommunicateServiceImpl communicateService;// 沟通记录
	@Autowired
	private DemandInterviewServiceImpl interviewService;// 面试
	@Autowired
	private TalentsRecommendServiceImpl recommendService;// 推荐
	@Autowired
	private DemandOfferRecordServiceImpl offerRecordService;// 入职
	@Autowired
	private WorkSearchServiceImpl workSearchService;// 工作记录
	@Autowired
	private CompanyBookServiceImpl companyBookService;// 企业通讯录
	@Autowired
	private TaskManagerServiceImpl taskManagerService;

	/**
	 * 进入tab标签
	 * 
	 * @return
	 */
	@RequestMapping(value = "/tab")
	public String tab() {
		return "sys/tab";
	}

	/**
	 * index默认页面显示
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login_default")
	public String login_default(Model model, CommonQuery query) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
		query.setStartDate(sdf.format(new Date()));
		query.setCount("4");
		List<DemandCommunicate> communicates = communicateService.getListBy(query);
		if (communicates.size() > 0) {
			model.addAttribute("communicates", communicates);
		}
		List<DemandInterview> interviews = interviewService.getListBy(query);
		if (interviews.size() > 0) {
			for (DemandInterview demandInterview : interviews) {
				String recommendId = demandInterview.getRecommendId();
				if (StringUtils.isNotEmpty(recommendId)) {
					TalentsRecommend recommend = recommendService.getById(recommendId);
					if (recommend != null) {
						demandInterview.setRecommend(recommend);
						demandInterview.setCompanyName(recommend.getCompany().getName());
						demandInterview.setPost(recommend.getPosts());
					}
				}
			}
			model.addAttribute("interviews", interviews);
		}
		List<DemandOfferRecord> offerRecords = offerRecordService.getListBy(query);
		if (offerRecords.size() > 0) {
			for (DemandOfferRecord demandOfferRecord : offerRecords) {
				String recommendId = demandOfferRecord.getRecommendId();
				if (StringUtils.isNotEmpty(recommendId)) {
					TalentsRecommend recommend = recommendService.getById(recommendId);
					if (recommend != null) {
						demandOfferRecord.setRecommend(recommend);
						demandOfferRecord.setCompanyName(recommend.getCompany().getName());
					}
				}
			}
			model.addAttribute("offerRecords", offerRecords);
		}
		query.setProbation(sdf.format(new Date()));
		List<DemandOfferRecord> periods = offerRecordService.getListBy(query);
		if (periods.size() > 0) {
			for (DemandOfferRecord demandOfferRecord : periods) {
				String recommendId = demandOfferRecord.getRecommendId();
				if (StringUtils.isNotEmpty(recommendId)) {
					TalentsRecommend recommend = recommendService.getById(recommendId);
					if (recommend != null) {
						demandOfferRecord.setRecommend(recommend);
						demandOfferRecord.setCompanyName(recommend.getCompany().getName());
					}
				}
			}
			model.addAttribute("periods", periods);
		}
		Date nsdate = RemindDateUtils.getCurrentDayStartTime();
		Date nedate = RemindDateUtils.getCurrentDayEndTime();
		query.setStartDate(sdf.format(nsdate));
		query.setEndDate(sdf.format(nedate));
		List<WorkSearch> workSearchs = workSearchService.getListBy(query);
		if (workSearchs.size() > 0) {
			model.addAttribute("workSearchs", workSearchs);
		}
		List<CompanyBook> companyBooks = companyBookService.getListBy(query);
		if (companyBooks.size() > 0) {
			model.addAttribute("companyBooks", companyBooks);
		}
		query.setEndDate(null);
		List<TaskManager> tasks = taskManagerService.getListBy(query);
		if (tasks.size() > 0) {
			model.addAttribute("tasks", tasks);
		}
		return "default/default";
	}

	/**
	 * 查询登录信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getUname")
	public Map<String, Object> getUname(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) request.getSession().getAttribute("user_in_session");
		List<Role> role = roleService.getByuId(user.getId());
		map.put("user", user);
		map.put("role", role);
		map.put("skin", user.getSKIN());
		return map;
	}

	@ResponseBody
	@RequestMapping("/setSKIN")
	public Integer setSKIN(String SKIN, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user_in_session");
		user.setSKIN(SKIN);
		userService.update(user);
		return 0;
	}

	@RequestMapping("/403")
	public String unauthorizedRole() {
		return "error/403";
	}

	@RequestMapping("/getIndexJson")
	@ResponseBody
	public JSONObject getIndexJson(CommonQuery query) {
		JSONObject json = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, String>> resumeLists = new ArrayList<Map<String, String>>();
		try {
			List<Map<String, String>> resumes = resumeService.getResumeForIndex(query);
			List<Map<String, String>> demandnext = communicateService.getCommunicateForIndex(query);
			if (resumes.size() > 0) {
				for (Map<String, String> maps : resumes) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("trueName", maps.get("trueName"));
					map.put("createDate", sdf.format(maps.get("createDate")));
					map.put("createUserName", maps.get("createUserName"));
					resumeLists.add(map);
				}
			}
			json.put("resumes", resumeLists);
			json.put("demandnext", demandnext);
			json.put("code", 200);
			json.put("success", "数据获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("msg", "数据获取失败");
		}
		return json;
	}
}
